package session;

import entity.Client;
import entity.IndicativeProductPrice;
import entity.SalesInquiry;
import entity.SalesInquiryItem;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Samuel
 */
@Stateful
@LocalBean
public class SalesInquiryManager {

    @PersistenceContext
    private EntityManager em;
    private SalesInquiry salesInquiry;
    private SalesInquiryItem salesInquiryItem;
    private Collection<SalesInquiry> salesInquiryList = new ArrayList<SalesInquiry>();
    private IndicativeProductPrice productPrice;
    private Collection<IndicativeProductPrice> productsList = new ArrayList<IndicativeProductPrice>();
    private Collection<Client> clients = new ArrayList<Client>();     //maybe to be removed when merged.

    //to be deleted/merged when intergrated
    public Collection<Client> getClients() {
        clients = new ArrayList<Client>();
        Query q = em.createQuery("Select c From Client c");
        for (Object o : q.getResultList()) {
            Client temp = (Client) o;
            clients.add(temp);
        }
        return clients;
    }

    //may be deleted/copied to ClientManager when merged with other people
    public Client getClient(Long id) {
        Client c;
        c = getEm().find(Client.class, id);

        return c;
    }

    public void createSalesInquiry() {
        salesInquiry = new SalesInquiry();
        Date today = new java.util.Date();
        Timestamp dateTime = new Timestamp(today.getTime());
        salesInquiry.setDateTime(dateTime);
    }

    public void addSalesInquiryItem(String productName, Integer numOfProduct, Double itemProductPrice) {
        salesInquiryItem = new SalesInquiryItem();
        salesInquiryItem.setProductName(productName);
        salesInquiryItem.setNumOfProduct(numOfProduct);
        salesInquiryItem.setProductPrice(itemProductPrice);
        salesInquiry.getSalesInquiryItems().add(salesInquiryItem);

    }

    public void editSalesInquiry(Long salesInquiryId, String status) {
        SalesInquiry s;
        s = em.find(SalesInquiry.class, salesInquiryId);
        s.setStatus(status);
        em.flush();

    }

    public Collection<SalesInquiryItem> getSalesInquiryItems() {

        return salesInquiry.getSalesInquiryItems();

    }

    public void persistSalesInquiry() {
        Double temp = calculateTotalPrice();
        temp = (Math.round(temp * 100)) / 100.0;
        salesInquiry.setTotalPrice(temp);
        em.persist(salesInquiry);
        em.flush();
    }

    public Double calculateTotalPrice() {

        Double tempTotal = 0.0;
        for (Object o : salesInquiry.getSalesInquiryItems()) {
            SalesInquiryItem temp = (SalesInquiryItem) o;
            tempTotal += temp.getProductPrice() * temp.getNumOfProduct();
        }
        return tempTotal;
    }

    /**
     * @return the salesInquiryList
     */
    public Collection<SalesInquiry> getSalesInquiryList() {

        salesInquiryList = new ArrayList<SalesInquiry>();
        Query q = em.createQuery("Select s From SalesInquiry s");
        for (Object o : q.getResultList()) {
            SalesInquiry temp = (SalesInquiry) o;
            salesInquiryList.add(temp);
        }

        return salesInquiryList;
    }

    public Collection<SalesInquiry> getSalesInquiryList(Long clientId) {

        salesInquiryList = new ArrayList<SalesInquiry>();
        Query q = em.createQuery("Select s From SalesInquiry s");
        for (Object o : q.getResultList()) {
            SalesInquiry temp = (SalesInquiry) o;
            if (temp.getClient().getId().equals(clientId)) {
                salesInquiryList.add(temp);
            }
        }

        return salesInquiryList;
    }

    /**
     * @return the em
     */
    public EntityManager getEm() {
        return em;
    }

    /**
     * @param em the em to set
     */
    public void setEm(EntityManager em) {
        this.em = em;
    }

    /**
     * @return the salesInquiry
     */
    public SalesInquiry getSalesInquiry() {
        return salesInquiry;
    }

    public SalesInquiry getSalesInquiry(Long id) {
        SalesInquiry temp;
        temp = em.find(SalesInquiry.class, id);

        return temp;
    }

    /**
     * @param salesInquiry the salesInquiry to set
     */
    public void setSalesInquiry(SalesInquiry salesInquiry) {
        this.salesInquiry = salesInquiry;
    }

    /**
     * @return the salesInquiryItem
     */
    public SalesInquiryItem getSalesInquiryItem() {
        return salesInquiryItem;
    }

    /**
     * @param salesInquiryItem the salesInquiryItem to set
     */
    public void setSalesInquiryItem(SalesInquiryItem salesInquiryItem) {
        this.salesInquiryItem = salesInquiryItem;
    }

    /**
     * @param salesInquiryList the salesInquiryList to set
     */
    public void setSalesInquiryList(Collection<SalesInquiry> salesInquiryList) {
        this.salesInquiryList = salesInquiryList;
    }

    /**
     * @return the productPrice
     */
    public IndicativeProductPrice getProductPrice() {
        return productPrice;
    }

    /**
     * @param productPrice the productPrice to set
     */
    public void setProductPrice(IndicativeProductPrice productPrice) {
        this.productPrice = productPrice;
    }

    /**
     * @return the productsList
     */
    public Collection<IndicativeProductPrice> getProductsList() {
        return productsList;
    }

    /**
     * @param productsList the productsList to set
     */
    public void setProductsList(Collection<IndicativeProductPrice> productsList) {
        this.productsList = productsList;
    }

    /**
     * @param clients the clients to set
     */
    public void setClients(Collection<Client> clients) {
        this.clients = clients;
    }
}
